package logic

import (
	"context"
	"errors"
	"pro-shop-manager/models/mysql"

	"pro-shop-manager/userrpc/internal/svc"
	"pro-shop-manager/userrpc/userrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type ShopAddLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewShopAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ShopAddLogic {
	return &ShopAddLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 商品添加
func (l *ShopAddLogic) ShopAdd(in *userrpc.ShopAddRequest) (*userrpc.ShopAddResponse, error) {
	// todo: add your logic here and delete this line

	var user mysql.User

	if err := user.UserFindById(mysql.DB, in.UserId); err != nil {
		return nil, errors.New("该用户不存在")
	}

	shop := &mysql.Shop{
		ShopName:  in.ShopName,
		ShopPrice: float64(in.ShopPrice),
		ShopStock: int(in.ShopStock),
		UserId:    int(in.UserId),
	}

	if err := shop.ShopAdd(mysql.DB); err != nil {
		return nil, err
	}

	return &userrpc.ShopAddResponse{
		ShopId: int64(shop.ID),
	}, nil
}
